﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

using SharedGenomics.Domain;
using SharedGenomics.Core;
using SharedGenomics.Workbench.Core;
using SharedGenomics.Annotations;

namespace SharedGenomics.Workbench.Views.WindowsForms
{
    /// <summary>
    /// A Windows Forms-based web browser, for use when recording annotation sessions.
    /// </summary>
    public partial class WebBrowserView : View, IWebBrowserView
    {
        public WebBrowserView()
        {
            InitializeComponent();
        }

        #region IWebBrowserView Members

        public string Url
        {
            get
            {
                return txtUrl.Text;
            }
            set
            {
                txtUrl.Text = value;
                webBrowser1.Navigate(value);
            }
        }

        #endregion

        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            DisableAnnotations();
            StartProgress();
            txtUrl.Text = e.Url.ToString();
        }

        private void StartProgress()
        {
            progressBar.Visible = true;
        }

        private void StopProgress()
        {
            progressBar.Visible = false;
        }

        private void cmdStop_Click(object sender, EventArgs e)
        {
            StopProgress();
            webBrowser1.Stop();
        }

        private void cmdRefresh_Click(object sender, EventArgs e)
        {
            webBrowser1.Refresh();
        }

        private void cmdBack_Click(object sender, EventArgs e)
        {
            DisableAnnotations();
            webBrowser1.GoBack();
        }

        private void cmdNext_Click(object sender, EventArgs e)
        {
            DisableAnnotations();
            webBrowser1.GoForward();
        }

        private void cmdGo_Click(object sender, EventArgs e)
        {
            DisableAnnotations();
            webBrowser1.Navigate(txtUrl.Text);
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            StopProgress();
            TakeSnapshot();
            EnableAnnotations();
        }

        private void DisableAnnotations()
        {
            commentToolStripButton.Enabled = false;
            isImportantToolStripButton.Enabled = false;
            // We disable when navigating, and the new page definitely won't be important yet as we've not told it to be.  Therefore, reset the button state.
            // Because the button is disabled at this point, it shouldn't change the state for the existing annotation for the page we're leaving.
            isImportantToolStripButton.Checked = false;
        }

        private void EnableAnnotations()
        {
            commentToolStripButton.Enabled = true;
            isImportantToolStripButton.Enabled = true;
        }

        private void TakeSnapshot()
        {
            // Don't snapshot blank pages
            if ("about:blank".Equals(webBrowser1.Url.ToString()))
                return;

            // Force an annotation shown.  TODO: What's the clean way?
            Application.DoEvents(); // A bit of a hack, but forces the display to complete
            Image loadedPageImage = this.GetImage();
            NavigationCompletedEventArgs ea = new NavigationCompletedEventArgs(loadedPageImage, this.webBrowser1.Url, this.webBrowser1.DocumentTitle);

            this.NavigationCompleted.Raise(this, ea);
        }

        private void WebBrowserView_Load(object sender, EventArgs e)
        {
            webBrowser1.DocumentTitleChanged += new EventHandler(webBrowser1_DocumentTitleChanged);
        }

        private void webBrowser1_DocumentTitleChanged(object sender, EventArgs e)
        {
            this.FindForm().Text = webBrowser1.DocumentTitle;
        }

        private Image GetImage()
        {
            Form form = webBrowser1.FindForm();
            return ScreenCapture.CaptureWindow(form.Handle);
        }

        public event EventHandler CommentButtonClicked;
        public event EventHandler<ViewedAnnotationIdChangedEventArgs> ViewedAnnotationIdChanged;
        public event EventHandler<ImportanceChangedEventArgs> ImportanceChanged;
        public event EventHandler<NavigationCompletedEventArgs> NavigationCompleted;

        private void isImportantToolStripButton_CheckedChanged(object sender, EventArgs e)
        {
            if (isImportantToolStripButton.Enabled)
            {
                ImportanceChangedEventArgs args = new ImportanceChangedEventArgs();
                args.Importance = isImportantToolStripButton.Checked;
                this.ImportanceChanged.Raise(this, args);
            }
        }

        private void commentToolStripButton_Click(object sender, EventArgs e)
        {
            this.CommentButtonClicked.Raise(this);
        }
    }
}
